home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / progargslib.lha / ProgArgs / Examples / Support / lists.c < prev    next >
C/C++ Source or Header  |  1995-04-08  |  1KB  |  63 lines

  1.  
  2. #include <proto/exec.h>
  3. #include "lists.h"
  4.  
  5. void *num_to_node(struct List *lst,ULONG num)
  6. {
  7.     struct Node *nd;
  8.  
  9.     for(nd = lst->lh_Head;
  10.         nd != NULL && nd->ln_Succ != NULL && num > 0;
  11.         nd = nd->ln_Succ, num--) ;
  12.  
  13.     if(nd == NULL || nd->ln_Succ == NULL) return NULL;
  14.     else return nd;
  15. }
  16.  
  17. ULONG node_to_num(struct List *lst,void *nd)
  18. {
  19.     struct Node *pos;
  20.     ULONG num = 0;
  21.  
  22.     if(!nd) return 0;
  23.  
  24.     for(pos = lst->lh_Head;
  25.         pos != NULL && pos->ln_Succ != NULL && pos != (struct Node *)nd;
  26.         pos = pos->ln_Succ, num++) ;
  27.  
  28.     if(pos == NULL || pos->ln_Succ == NULL) return ~0;
  29.     else return num;
  30. }
  31.  
  32. void *head_node(struct List *lst)
  33. {
  34.     if(lst == NULL || lst->lh_Head == NULL || lst->lh_Head->ln_Succ == NULL)
  35.         return NULL;
  36.  
  37.     return lst->lh_Head;
  38. }
  39.  
  40. void *tail_node(struct List *lst)
  41. {
  42.     if(lst == NULL || lst->lh_TailPred == NULL || lst->lh_TailPred->ln_Pred == NULL)
  43.         return NULL;
  44.  
  45.     return lst->lh_TailPred;
  46. }
  47.  
  48. void *next_node(void *nd)
  49. {
  50.     if( nd == NULL || ((struct Node *)nd)->ln_Succ == NULL
  51.         || ((struct Node *)nd)->ln_Succ->ln_Succ == NULL ) return NULL;
  52.  
  53.     return ((struct Node *)nd)->ln_Succ;
  54. }
  55.  
  56. void *prev_node(void *nd)
  57. {
  58.     if( nd == NULL || ((struct Node *)nd)->ln_Pred == NULL
  59.         || ((struct Node *)nd)->ln_Pred->ln_Pred == NULL ) return NULL;
  60.  
  61.     return ((struct Node *)nd)->ln_Pred;
  62. }
  63.